Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  Sony MemoryStick support
  4 *
  5 *  Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
  6 *
  7 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
  8 * that made this driver possible.
  9 */
 10
 11#include <linux/memstick.h>
 12#include <linux/idr.h>
 13#include <linux/fs.h>
 14#include <linux/delay.h>
 15#include <linux/slab.h>
 16#include <linux/module.h>
 17#include <linux/pm_runtime.h>
 18
 19#define DRIVER_NAME "memstick"
 20
 21static unsigned int cmd_retries = 3;
 22module_param(cmd_retries, uint, 0644);
 23
 24static struct workqueue_struct *workqueue;
 25static DEFINE_IDR(memstick_host_idr);
 26static DEFINE_SPINLOCK(memstick_host_lock);
 27
 28static int memstick_dev_match(struct memstick_dev *card,
 29			      struct memstick_device_id *id)
 30{
 31	if (id->match_flags & MEMSTICK_MATCH_ALL) {
 32		if ((id->type == card->id.type)
 33		    && (id->category == card->id.category)
 34		    && (id->class == card->id.class))
 35			return 1;
 36	}
 37
 38	return 0;
 39}
 40
 41static int memstick_bus_match(struct device *dev, struct device_driver *drv)
 42{
 43	struct memstick_dev *card = container_of(dev, struct memstick_dev,
 44						 dev);
 45	struct memstick_driver *ms_drv = container_of(drv,
 46						      struct memstick_driver,
 47						      driver);
 48	struct memstick_device_id *ids = ms_drv->id_table;
 49
 50	if (ids) {
 51		while (ids->match_flags) {
 52			if (memstick_dev_match(card, ids))
 53				return 1;
 54			++ids;
 55		}
 56	}
 57	return 0;
 58}
 59
 60static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
 61{
 62	struct memstick_dev *card = container_of(dev, struct memstick_dev,
 63						  dev);
 64
 65	if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
 66		return -ENOMEM;
 67
 68	if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
 69		return -ENOMEM;
 70
 71	if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
 72		return -ENOMEM;
 73
 74	return 0;
 75}
 76
 77static int memstick_device_probe(struct device *dev)
 78{
 79	struct memstick_dev *card = container_of(dev, struct memstick_dev,
 80						 dev);
 81	struct memstick_driver *drv = container_of(dev->driver,
 82						   struct memstick_driver,
 83						   driver);
 84	int rc = -ENODEV;
 85
 86	if (dev->driver && drv->probe) {
 87		rc = drv->probe(card);
 88		if (!rc)
 89			get_device(dev);
 90	}
 91	return rc;
 92}
 93
 94static int memstick_device_remove(struct device *dev)
 95{
 96	struct memstick_dev *card = container_of(dev, struct memstick_dev,
 97						  dev);
 98	struct memstick_driver *drv = container_of(dev->driver,
 99						   struct memstick_driver,
100						   driver);
101
102	if (dev->driver && drv->remove) {
103		drv->remove(card);
104		card->dev.driver = NULL;
105	}
106
107	put_device(dev);
108	return 0;
109}
110
111#ifdef CONFIG_PM
112
113static int memstick_device_suspend(struct device *dev, pm_message_t state)
114{
115	struct memstick_dev *card = container_of(dev, struct memstick_dev,
116						  dev);
117	struct memstick_driver *drv = container_of(dev->driver,
118						   struct memstick_driver,
119						   driver);
120
121	if (dev->driver && drv->suspend)
122		return drv->suspend(card, state);
123	return 0;
124}
125
126static int memstick_device_resume(struct device *dev)
127{
128	struct memstick_dev *card = container_of(dev, struct memstick_dev,
129						  dev);
130	struct memstick_driver *drv = container_of(dev->driver,
131						   struct memstick_driver,
132						   driver);
133
134	if (dev->driver && drv->resume)
135		return drv->resume(card);
136	return 0;
137}
138
139#else
140
141#define memstick_device_suspend NULL
142#define memstick_device_resume NULL
143
144#endif /* CONFIG_PM */
145
146#define MEMSTICK_ATTR(name, format)                                           \
147static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
148			    char *buf)                                        \
149{                                                                             \
150	struct memstick_dev *card = container_of(dev, struct memstick_dev,    \
151						 dev);                        \
152	return sprintf(buf, format, card->id.name);                           \
153}                                                                             \
154static DEVICE_ATTR_RO(name);
155
156MEMSTICK_ATTR(type, "%02X");
157MEMSTICK_ATTR(category, "%02X");
158MEMSTICK_ATTR(class, "%02X");
159
160static struct attribute *memstick_dev_attrs[] = {
161	&dev_attr_type.attr,
162	&dev_attr_category.attr,
163	&dev_attr_class.attr,
164	NULL,
165};
166ATTRIBUTE_GROUPS(memstick_dev);
167
168static struct bus_type memstick_bus_type = {
169	.name           = "memstick",
170	.dev_groups	= memstick_dev_groups,
171	.match          = memstick_bus_match,
172	.uevent         = memstick_uevent,
173	.probe          = memstick_device_probe,
174	.remove         = memstick_device_remove,
175	.suspend        = memstick_device_suspend,
176	.resume         = memstick_device_resume
177};
178
179static void memstick_free(struct device *dev)
180{
181	struct memstick_host *host = container_of(dev, struct memstick_host,
182						  dev);
183	kfree(host);
184}
185
186static struct class memstick_host_class = {
187	.name        = "memstick_host",
188	.dev_release = memstick_free
189};
190
191static void memstick_free_card(struct device *dev)
192{
193	struct memstick_dev *card = container_of(dev, struct memstick_dev,
194						 dev);
195	kfree(card);
196}
197
198static int memstick_dummy_check(struct memstick_dev *card)
199{
200	return 0;
201}
202
203/**
204 * memstick_detect_change - schedule media detection on memstick host
205 * @host - host to use
206 */
207void memstick_detect_change(struct memstick_host *host)
208{
209	queue_work(workqueue, &host->media_checker);
210}
211EXPORT_SYMBOL(memstick_detect_change);
212
213/**
214 * memstick_next_req - called by host driver to obtain next request to process
215 * @host - host to use
216 * @mrq - pointer to stick the request to
217 *
218 * Host calls this function from idle state (*mrq == NULL) or after finishing
219 * previous request (*mrq should point to it). If previous request was
220 * unsuccessful, it is retried for predetermined number of times. Return value
221 * of 0 means that new request was assigned to the host.
222 */
223int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
224{
225	int rc = -ENXIO;
226
227	if ((*mrq) && (*mrq)->error && host->retries) {
228		(*mrq)->error = rc;
229		host->retries--;
230		return 0;
231	}
232
233	if (host->card && host->card->next_request)
234		rc = host->card->next_request(host->card, mrq);
235
236	if (!rc)
237		host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
238	else
239		*mrq = NULL;
240
241	return rc;
242}
243EXPORT_SYMBOL(memstick_next_req);
244
245/**
246 * memstick_new_req - notify the host that some requests are pending
247 * @host - host to use
248 */
249void memstick_new_req(struct memstick_host *host)
250{
251	if (host->card) {
252		host->retries = cmd_retries;
253		reinit_completion(&host->card->mrq_complete);
254		host->request(host);
255	}
256}
257EXPORT_SYMBOL(memstick_new_req);
258
259/**
260 * memstick_init_req_sg - set request fields needed for bulk data transfer
261 * @mrq - request to use
262 * @tpc - memstick Transport Protocol Command
263 * @sg - TPC argument
264 */
265void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
266			  const struct scatterlist *sg)
267{
268	mrq->tpc = tpc;
269	if (tpc & 8)
270		mrq->data_dir = WRITE;
271	else
272		mrq->data_dir = READ;
273
274	mrq->sg = *sg;
275	mrq->long_data = 1;
276
277	if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
278		mrq->need_card_int = 1;
279	else
280		mrq->need_card_int = 0;
281}
282EXPORT_SYMBOL(memstick_init_req_sg);
283
284/**
285 * memstick_init_req - set request fields needed for short data transfer
286 * @mrq - request to use
287 * @tpc - memstick Transport Protocol Command
288 * @buf - TPC argument buffer
289 * @length - TPC argument size
290 *
291 * The intended use of this function (transfer of data items several bytes
292 * in size) allows us to just copy the value between request structure and
293 * user supplied buffer.
294 */
295void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
296		       const void *buf, size_t length)
297{
298	mrq->tpc = tpc;
299	if (tpc & 8)
300		mrq->data_dir = WRITE;
301	else
302		mrq->data_dir = READ;
303
304	mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
305	if (mrq->data_dir == WRITE)
306		memcpy(mrq->data, buf, mrq->data_len);
307
308	mrq->long_data = 0;
309
310	if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
311		mrq->need_card_int = 1;
312	else
313		mrq->need_card_int = 0;
314}
315EXPORT_SYMBOL(memstick_init_req);
316
317/*
318 * Functions prefixed with "h_" are protocol callbacks. They can be called from
319 * interrupt context. Return value of 0 means that request processing is still
320 * ongoing, while special error value of -EAGAIN means that current request is
321 * finished (and request processor should come back some time later).
322 */
323
324static int h_memstick_read_dev_id(struct memstick_dev *card,
325				  struct memstick_request **mrq)
326{
327	struct ms_id_register id_reg;
328
329	if (!(*mrq)) {
330		memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, &id_reg,
331				  sizeof(struct ms_id_register));
332		*mrq = &card->current_mrq;
333		return 0;
 
 
 
 
 
 
 
 
 
 
 
334	}
335	if (!(*mrq)->error) {
336		memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
337		card->id.match_flags = MEMSTICK_MATCH_ALL;
338		card->id.type = id_reg.type;
339		card->id.category = id_reg.category;
340		card->id.class = id_reg.class;
341		dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
342	}
343	complete(&card->mrq_complete);
344	return -EAGAIN;
345}
346
347static int h_memstick_set_rw_addr(struct memstick_dev *card,
348				  struct memstick_request **mrq)
349{
350	if (!(*mrq)) {
351		memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS,
352				  (char *)&card->reg_addr,
353				  sizeof(card->reg_addr));
354		*mrq = &card->current_mrq;
355		return 0;
356	} else {
357		complete(&card->mrq_complete);
358		return -EAGAIN;
359	}
360}
361
362/**
363 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
364 *                        complete
365 * @card - media device to use
366 */
367int memstick_set_rw_addr(struct memstick_dev *card)
368{
369	card->next_request = h_memstick_set_rw_addr;
370	memstick_new_req(card->host);
371	wait_for_completion(&card->mrq_complete);
372
373	return card->current_mrq.error;
374}
375EXPORT_SYMBOL(memstick_set_rw_addr);
376
377static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
378{
379	struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
380					    GFP_KERNEL);
381	struct memstick_dev *old_card = host->card;
382	struct ms_id_register id_reg;
383
384	if (card) {
385		card->host = host;
386		dev_set_name(&card->dev, "%s", dev_name(&host->dev));
387		card->dev.parent = &host->dev;
388		card->dev.bus = &memstick_bus_type;
389		card->dev.release = memstick_free_card;
390		card->check = memstick_dummy_check;
391
392		card->reg_addr.r_offset = offsetof(struct ms_register, id);
393		card->reg_addr.r_length = sizeof(id_reg);
394		card->reg_addr.w_offset = offsetof(struct ms_register, id);
395		card->reg_addr.w_length = sizeof(id_reg);
396
397		init_completion(&card->mrq_complete);
398
399		host->card = card;
400		if (memstick_set_rw_addr(card))
401			goto err_out;
402
403		card->next_request = h_memstick_read_dev_id;
404		memstick_new_req(host);
405		wait_for_completion(&card->mrq_complete);
406
407		if (card->current_mrq.error)
408			goto err_out;
409	}
410	host->card = old_card;
411	return card;
412err_out:
413	host->card = old_card;
414	kfree(card);
415	return NULL;
416}
417
418static int memstick_power_on(struct memstick_host *host)
419{
420	int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
421
422	if (!rc)
423		rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
424
425	return rc;
426}
427
428static void memstick_check(struct work_struct *work)
429{
430	struct memstick_host *host = container_of(work, struct memstick_host,
431						  media_checker);
432	struct memstick_dev *card;
433
434	dev_dbg(&host->dev, "memstick_check started\n");
435	pm_runtime_get_noresume(host->dev.parent);
436	mutex_lock(&host->lock);
437	if (!host->card) {
438		if (memstick_power_on(host))
439			goto out_power_off;
440	} else if (host->card->stop)
441		host->card->stop(host->card);
442
443	if (host->removing)
444		goto out_power_off;
445
446	card = memstick_alloc_card(host);
447
448	if (!card) {
449		if (host->card) {
450			device_unregister(&host->card->dev);
451			host->card = NULL;
452		}
453	} else {
454		dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
455			card->id.type, card->id.category, card->id.class);
456		if (host->card) {
457			if (memstick_set_rw_addr(host->card)
458			    || !memstick_dev_match(host->card, &card->id)
459			    || !(host->card->check(host->card))) {
460				device_unregister(&host->card->dev);
461				host->card = NULL;
462			} else if (host->card->start)
463				host->card->start(host->card);
464		}
465
466		if (!host->card) {
467			host->card = card;
468			if (device_register(&card->dev)) {
469				put_device(&card->dev);
 
470				host->card = NULL;
471			}
472		} else
473			kfree(card);
474	}
475
476out_power_off:
477	if (!host->card)
478		host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
479
480	mutex_unlock(&host->lock);
481	pm_runtime_put(host->dev.parent);
482	dev_dbg(&host->dev, "memstick_check finished\n");
483}
484
485/**
486 * memstick_alloc_host - allocate a memstick_host structure
487 * @extra: size of the user private data to allocate
488 * @dev: parent device of the host
489 */
490struct memstick_host *memstick_alloc_host(unsigned int extra,
491					  struct device *dev)
492{
493	struct memstick_host *host;
494
495	host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
496	if (host) {
497		mutex_init(&host->lock);
498		INIT_WORK(&host->media_checker, memstick_check);
499		host->dev.class = &memstick_host_class;
500		host->dev.parent = dev;
501		device_initialize(&host->dev);
502	}
503	return host;
504}
505EXPORT_SYMBOL(memstick_alloc_host);
506
507/**
508 * memstick_add_host - start request processing on memstick host
509 * @host - host to use
510 */
511int memstick_add_host(struct memstick_host *host)
512{
513	int rc;
514
515	idr_preload(GFP_KERNEL);
516	spin_lock(&memstick_host_lock);
517
518	rc = idr_alloc(&memstick_host_idr, host, 0, 0, GFP_NOWAIT);
519	if (rc >= 0)
520		host->id = rc;
521
522	spin_unlock(&memstick_host_lock);
523	idr_preload_end();
524	if (rc < 0)
525		return rc;
526
527	dev_set_name(&host->dev, "memstick%u", host->id);
528
529	rc = device_add(&host->dev);
530	if (rc) {
531		spin_lock(&memstick_host_lock);
532		idr_remove(&memstick_host_idr, host->id);
533		spin_unlock(&memstick_host_lock);
534		return rc;
535	}
536
537	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
538	memstick_detect_change(host);
539	return 0;
540}
541EXPORT_SYMBOL(memstick_add_host);
542
543/**
544 * memstick_remove_host - stop request processing on memstick host
545 * @host - host to use
546 */
547void memstick_remove_host(struct memstick_host *host)
548{
549	host->removing = 1;
550	flush_workqueue(workqueue);
551	mutex_lock(&host->lock);
552	if (host->card)
553		device_unregister(&host->card->dev);
554	host->card = NULL;
555	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
556	mutex_unlock(&host->lock);
557
558	spin_lock(&memstick_host_lock);
559	idr_remove(&memstick_host_idr, host->id);
560	spin_unlock(&memstick_host_lock);
561	device_del(&host->dev);
562}
563EXPORT_SYMBOL(memstick_remove_host);
564
565/**
566 * memstick_free_host - free memstick host
567 * @host - host to use
568 */
569void memstick_free_host(struct memstick_host *host)
570{
571	mutex_destroy(&host->lock);
572	put_device(&host->dev);
573}
574EXPORT_SYMBOL(memstick_free_host);
575
576/**
577 * memstick_suspend_host - notify bus driver of host suspension
578 * @host - host to use
579 */
580void memstick_suspend_host(struct memstick_host *host)
581{
582	mutex_lock(&host->lock);
583	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
584	mutex_unlock(&host->lock);
585}
586EXPORT_SYMBOL(memstick_suspend_host);
587
588/**
589 * memstick_resume_host - notify bus driver of host resumption
590 * @host - host to use
591 */
592void memstick_resume_host(struct memstick_host *host)
593{
594	int rc = 0;
595
596	mutex_lock(&host->lock);
597	if (host->card)
598		rc = memstick_power_on(host);
599	mutex_unlock(&host->lock);
600
601	if (!rc)
602		memstick_detect_change(host);
603}
604EXPORT_SYMBOL(memstick_resume_host);
605
606int memstick_register_driver(struct memstick_driver *drv)
607{
608	drv->driver.bus = &memstick_bus_type;
609
610	return driver_register(&drv->driver);
611}
612EXPORT_SYMBOL(memstick_register_driver);
613
614void memstick_unregister_driver(struct memstick_driver *drv)
615{
616	driver_unregister(&drv->driver);
617}
618EXPORT_SYMBOL(memstick_unregister_driver);
619
620
621static int __init memstick_init(void)
622{
623	int rc;
624
625	workqueue = create_freezable_workqueue("kmemstick");
626	if (!workqueue)
627		return -ENOMEM;
628
629	rc = bus_register(&memstick_bus_type);
630	if (rc)
631		goto error_destroy_workqueue;
632
633	rc = class_register(&memstick_host_class);
634	if (rc)
635		goto error_bus_unregister;
636
637	return 0;
638
639error_bus_unregister:
640	bus_unregister(&memstick_bus_type);
641error_destroy_workqueue:
642	destroy_workqueue(workqueue);
643
644	return rc;
645}
646
647static void __exit memstick_exit(void)
648{
649	class_unregister(&memstick_host_class);
650	bus_unregister(&memstick_bus_type);
651	destroy_workqueue(workqueue);
652	idr_destroy(&memstick_host_idr);
653}
654
655module_init(memstick_init);
656module_exit(memstick_exit);
657
658MODULE_AUTHOR("Alex Dubov");
659MODULE_LICENSE("GPL");
660MODULE_DESCRIPTION("Sony MemoryStick core driver");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 *  Sony MemoryStick support
  4 *
  5 *  Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
  6 *
  7 * Special thanks to Carlos Corbacho for providing various MemoryStick cards
  8 * that made this driver possible.
  9 */
 10
 11#include <linux/memstick.h>
 12#include <linux/idr.h>
 13#include <linux/fs.h>
 14#include <linux/delay.h>
 15#include <linux/slab.h>
 16#include <linux/module.h>
 17#include <linux/pm_runtime.h>
 18
 19#define DRIVER_NAME "memstick"
 20
 21static unsigned int cmd_retries = 3;
 22module_param(cmd_retries, uint, 0644);
 23
 24static struct workqueue_struct *workqueue;
 25static DEFINE_IDR(memstick_host_idr);
 26static DEFINE_SPINLOCK(memstick_host_lock);
 27
 28static int memstick_dev_match(struct memstick_dev *card,
 29			      struct memstick_device_id *id)
 30{
 31	if (id->match_flags & MEMSTICK_MATCH_ALL) {
 32		if ((id->type == card->id.type)
 33		    && (id->category == card->id.category)
 34		    && (id->class == card->id.class))
 35			return 1;
 36	}
 37
 38	return 0;
 39}
 40
 41static int memstick_bus_match(struct device *dev, struct device_driver *drv)
 42{
 43	struct memstick_dev *card = container_of(dev, struct memstick_dev,
 44						 dev);
 45	struct memstick_driver *ms_drv = container_of(drv,
 46						      struct memstick_driver,
 47						      driver);
 48	struct memstick_device_id *ids = ms_drv->id_table;
 49
 50	if (ids) {
 51		while (ids->match_flags) {
 52			if (memstick_dev_match(card, ids))
 53				return 1;
 54			++ids;
 55		}
 56	}
 57	return 0;
 58}
 59
 60static int memstick_uevent(struct device *dev, struct kobj_uevent_env *env)
 61{
 62	struct memstick_dev *card = container_of(dev, struct memstick_dev,
 63						  dev);
 64
 65	if (add_uevent_var(env, "MEMSTICK_TYPE=%02X", card->id.type))
 66		return -ENOMEM;
 67
 68	if (add_uevent_var(env, "MEMSTICK_CATEGORY=%02X", card->id.category))
 69		return -ENOMEM;
 70
 71	if (add_uevent_var(env, "MEMSTICK_CLASS=%02X", card->id.class))
 72		return -ENOMEM;
 73
 74	return 0;
 75}
 76
 77static int memstick_device_probe(struct device *dev)
 78{
 79	struct memstick_dev *card = container_of(dev, struct memstick_dev,
 80						 dev);
 81	struct memstick_driver *drv = container_of(dev->driver,
 82						   struct memstick_driver,
 83						   driver);
 84	int rc = -ENODEV;
 85
 86	if (dev->driver && drv->probe) {
 87		rc = drv->probe(card);
 88		if (!rc)
 89			get_device(dev);
 90	}
 91	return rc;
 92}
 93
 94static int memstick_device_remove(struct device *dev)
 95{
 96	struct memstick_dev *card = container_of(dev, struct memstick_dev,
 97						  dev);
 98	struct memstick_driver *drv = container_of(dev->driver,
 99						   struct memstick_driver,
100						   driver);
101
102	if (dev->driver && drv->remove) {
103		drv->remove(card);
104		card->dev.driver = NULL;
105	}
106
107	put_device(dev);
108	return 0;
109}
110
111#ifdef CONFIG_PM
112
113static int memstick_device_suspend(struct device *dev, pm_message_t state)
114{
115	struct memstick_dev *card = container_of(dev, struct memstick_dev,
116						  dev);
117	struct memstick_driver *drv = container_of(dev->driver,
118						   struct memstick_driver,
119						   driver);
120
121	if (dev->driver && drv->suspend)
122		return drv->suspend(card, state);
123	return 0;
124}
125
126static int memstick_device_resume(struct device *dev)
127{
128	struct memstick_dev *card = container_of(dev, struct memstick_dev,
129						  dev);
130	struct memstick_driver *drv = container_of(dev->driver,
131						   struct memstick_driver,
132						   driver);
133
134	if (dev->driver && drv->resume)
135		return drv->resume(card);
136	return 0;
137}
138
139#else
140
141#define memstick_device_suspend NULL
142#define memstick_device_resume NULL
143
144#endif /* CONFIG_PM */
145
146#define MEMSTICK_ATTR(name, format)                                           \
147static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
148			    char *buf)                                        \
149{                                                                             \
150	struct memstick_dev *card = container_of(dev, struct memstick_dev,    \
151						 dev);                        \
152	return sprintf(buf, format, card->id.name);                           \
153}                                                                             \
154static DEVICE_ATTR_RO(name);
155
156MEMSTICK_ATTR(type, "%02X");
157MEMSTICK_ATTR(category, "%02X");
158MEMSTICK_ATTR(class, "%02X");
159
160static struct attribute *memstick_dev_attrs[] = {
161	&dev_attr_type.attr,
162	&dev_attr_category.attr,
163	&dev_attr_class.attr,
164	NULL,
165};
166ATTRIBUTE_GROUPS(memstick_dev);
167
168static struct bus_type memstick_bus_type = {
169	.name           = "memstick",
170	.dev_groups	= memstick_dev_groups,
171	.match          = memstick_bus_match,
172	.uevent         = memstick_uevent,
173	.probe          = memstick_device_probe,
174	.remove         = memstick_device_remove,
175	.suspend        = memstick_device_suspend,
176	.resume         = memstick_device_resume
177};
178
179static void memstick_free(struct device *dev)
180{
181	struct memstick_host *host = container_of(dev, struct memstick_host,
182						  dev);
183	kfree(host);
184}
185
186static struct class memstick_host_class = {
187	.name        = "memstick_host",
188	.dev_release = memstick_free
189};
190
191static void memstick_free_card(struct device *dev)
192{
193	struct memstick_dev *card = container_of(dev, struct memstick_dev,
194						 dev);
195	kfree(card);
196}
197
198static int memstick_dummy_check(struct memstick_dev *card)
199{
200	return 0;
201}
202
203/**
204 * memstick_detect_change - schedule media detection on memstick host
205 * @host - host to use
206 */
207void memstick_detect_change(struct memstick_host *host)
208{
209	queue_work(workqueue, &host->media_checker);
210}
211EXPORT_SYMBOL(memstick_detect_change);
212
213/**
214 * memstick_next_req - called by host driver to obtain next request to process
215 * @host - host to use
216 * @mrq - pointer to stick the request to
217 *
218 * Host calls this function from idle state (*mrq == NULL) or after finishing
219 * previous request (*mrq should point to it). If previous request was
220 * unsuccessful, it is retried for predetermined number of times. Return value
221 * of 0 means that new request was assigned to the host.
222 */
223int memstick_next_req(struct memstick_host *host, struct memstick_request **mrq)
224{
225	int rc = -ENXIO;
226
227	if ((*mrq) && (*mrq)->error && host->retries) {
228		(*mrq)->error = rc;
229		host->retries--;
230		return 0;
231	}
232
233	if (host->card && host->card->next_request)
234		rc = host->card->next_request(host->card, mrq);
235
236	if (!rc)
237		host->retries = cmd_retries > 1 ? cmd_retries - 1 : 1;
238	else
239		*mrq = NULL;
240
241	return rc;
242}
243EXPORT_SYMBOL(memstick_next_req);
244
245/**
246 * memstick_new_req - notify the host that some requests are pending
247 * @host - host to use
248 */
249void memstick_new_req(struct memstick_host *host)
250{
251	if (host->card) {
252		host->retries = cmd_retries;
253		reinit_completion(&host->card->mrq_complete);
254		host->request(host);
255	}
256}
257EXPORT_SYMBOL(memstick_new_req);
258
259/**
260 * memstick_init_req_sg - set request fields needed for bulk data transfer
261 * @mrq - request to use
262 * @tpc - memstick Transport Protocol Command
263 * @sg - TPC argument
264 */
265void memstick_init_req_sg(struct memstick_request *mrq, unsigned char tpc,
266			  const struct scatterlist *sg)
267{
268	mrq->tpc = tpc;
269	if (tpc & 8)
270		mrq->data_dir = WRITE;
271	else
272		mrq->data_dir = READ;
273
274	mrq->sg = *sg;
275	mrq->long_data = 1;
276
277	if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
278		mrq->need_card_int = 1;
279	else
280		mrq->need_card_int = 0;
281}
282EXPORT_SYMBOL(memstick_init_req_sg);
283
284/**
285 * memstick_init_req - set request fields needed for short data transfer
286 * @mrq - request to use
287 * @tpc - memstick Transport Protocol Command
288 * @buf - TPC argument buffer
289 * @length - TPC argument size
290 *
291 * The intended use of this function (transfer of data items several bytes
292 * in size) allows us to just copy the value between request structure and
293 * user supplied buffer.
294 */
295void memstick_init_req(struct memstick_request *mrq, unsigned char tpc,
296		       const void *buf, size_t length)
297{
298	mrq->tpc = tpc;
299	if (tpc & 8)
300		mrq->data_dir = WRITE;
301	else
302		mrq->data_dir = READ;
303
304	mrq->data_len = length > sizeof(mrq->data) ? sizeof(mrq->data) : length;
305	if (mrq->data_dir == WRITE)
306		memcpy(mrq->data, buf, mrq->data_len);
307
308	mrq->long_data = 0;
309
310	if (tpc == MS_TPC_SET_CMD || tpc == MS_TPC_EX_SET_CMD)
311		mrq->need_card_int = 1;
312	else
313		mrq->need_card_int = 0;
314}
315EXPORT_SYMBOL(memstick_init_req);
316
317/*
318 * Functions prefixed with "h_" are protocol callbacks. They can be called from
319 * interrupt context. Return value of 0 means that request processing is still
320 * ongoing, while special error value of -EAGAIN means that current request is
321 * finished (and request processor should come back some time later).
322 */
323
324static int h_memstick_read_dev_id(struct memstick_dev *card,
325				  struct memstick_request **mrq)
326{
327	struct ms_id_register id_reg;
328
329	if (!(*mrq)) {
330		memstick_init_req(&card->current_mrq, MS_TPC_READ_REG, &id_reg,
331				  sizeof(struct ms_id_register));
332		*mrq = &card->current_mrq;
333		return 0;
334	} else {
335		if (!(*mrq)->error) {
336			memcpy(&id_reg, (*mrq)->data, sizeof(id_reg));
337			card->id.match_flags = MEMSTICK_MATCH_ALL;
338			card->id.type = id_reg.type;
339			card->id.category = id_reg.category;
340			card->id.class = id_reg.class;
341			dev_dbg(&card->dev, "if_mode = %02x\n", id_reg.if_mode);
342		}
343		complete(&card->mrq_complete);
344		return -EAGAIN;
345	}
 
 
 
 
 
 
 
 
 
 
346}
347
348static int h_memstick_set_rw_addr(struct memstick_dev *card,
349				  struct memstick_request **mrq)
350{
351	if (!(*mrq)) {
352		memstick_init_req(&card->current_mrq, MS_TPC_SET_RW_REG_ADRS,
353				  (char *)&card->reg_addr,
354				  sizeof(card->reg_addr));
355		*mrq = &card->current_mrq;
356		return 0;
357	} else {
358		complete(&card->mrq_complete);
359		return -EAGAIN;
360	}
361}
362
363/**
364 * memstick_set_rw_addr - issue SET_RW_REG_ADDR request and wait for it to
365 *                        complete
366 * @card - media device to use
367 */
368int memstick_set_rw_addr(struct memstick_dev *card)
369{
370	card->next_request = h_memstick_set_rw_addr;
371	memstick_new_req(card->host);
372	wait_for_completion(&card->mrq_complete);
373
374	return card->current_mrq.error;
375}
376EXPORT_SYMBOL(memstick_set_rw_addr);
377
378static struct memstick_dev *memstick_alloc_card(struct memstick_host *host)
379{
380	struct memstick_dev *card = kzalloc(sizeof(struct memstick_dev),
381					    GFP_KERNEL);
382	struct memstick_dev *old_card = host->card;
383	struct ms_id_register id_reg;
384
385	if (card) {
386		card->host = host;
387		dev_set_name(&card->dev, "%s", dev_name(&host->dev));
388		card->dev.parent = &host->dev;
389		card->dev.bus = &memstick_bus_type;
390		card->dev.release = memstick_free_card;
391		card->check = memstick_dummy_check;
392
393		card->reg_addr.r_offset = offsetof(struct ms_register, id);
394		card->reg_addr.r_length = sizeof(id_reg);
395		card->reg_addr.w_offset = offsetof(struct ms_register, id);
396		card->reg_addr.w_length = sizeof(id_reg);
397
398		init_completion(&card->mrq_complete);
399
400		host->card = card;
401		if (memstick_set_rw_addr(card))
402			goto err_out;
403
404		card->next_request = h_memstick_read_dev_id;
405		memstick_new_req(host);
406		wait_for_completion(&card->mrq_complete);
407
408		if (card->current_mrq.error)
409			goto err_out;
410	}
411	host->card = old_card;
412	return card;
413err_out:
414	host->card = old_card;
415	kfree(card);
416	return NULL;
417}
418
419static int memstick_power_on(struct memstick_host *host)
420{
421	int rc = host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_ON);
422
423	if (!rc)
424		rc = host->set_param(host, MEMSTICK_INTERFACE, MEMSTICK_SERIAL);
425
426	return rc;
427}
428
429static void memstick_check(struct work_struct *work)
430{
431	struct memstick_host *host = container_of(work, struct memstick_host,
432						  media_checker);
433	struct memstick_dev *card;
434
435	dev_dbg(&host->dev, "memstick_check started\n");
436	pm_runtime_get_noresume(host->dev.parent);
437	mutex_lock(&host->lock);
438	if (!host->card) {
439		if (memstick_power_on(host))
440			goto out_power_off;
441	} else if (host->card->stop)
442		host->card->stop(host->card);
443
 
 
 
444	card = memstick_alloc_card(host);
445
446	if (!card) {
447		if (host->card) {
448			device_unregister(&host->card->dev);
449			host->card = NULL;
450		}
451	} else {
452		dev_dbg(&host->dev, "new card %02x, %02x, %02x\n",
453			card->id.type, card->id.category, card->id.class);
454		if (host->card) {
455			if (memstick_set_rw_addr(host->card)
456			    || !memstick_dev_match(host->card, &card->id)
457			    || !(host->card->check(host->card))) {
458				device_unregister(&host->card->dev);
459				host->card = NULL;
460			} else if (host->card->start)
461				host->card->start(host->card);
462		}
463
464		if (!host->card) {
465			host->card = card;
466			if (device_register(&card->dev)) {
467				put_device(&card->dev);
468				kfree(host->card);
469				host->card = NULL;
470			}
471		} else
472			kfree(card);
473	}
474
475out_power_off:
476	if (!host->card)
477		host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
478
479	mutex_unlock(&host->lock);
480	pm_runtime_put(host->dev.parent);
481	dev_dbg(&host->dev, "memstick_check finished\n");
482}
483
484/**
485 * memstick_alloc_host - allocate a memstick_host structure
486 * @extra: size of the user private data to allocate
487 * @dev: parent device of the host
488 */
489struct memstick_host *memstick_alloc_host(unsigned int extra,
490					  struct device *dev)
491{
492	struct memstick_host *host;
493
494	host = kzalloc(sizeof(struct memstick_host) + extra, GFP_KERNEL);
495	if (host) {
496		mutex_init(&host->lock);
497		INIT_WORK(&host->media_checker, memstick_check);
498		host->dev.class = &memstick_host_class;
499		host->dev.parent = dev;
500		device_initialize(&host->dev);
501	}
502	return host;
503}
504EXPORT_SYMBOL(memstick_alloc_host);
505
506/**
507 * memstick_add_host - start request processing on memstick host
508 * @host - host to use
509 */
510int memstick_add_host(struct memstick_host *host)
511{
512	int rc;
513
514	idr_preload(GFP_KERNEL);
515	spin_lock(&memstick_host_lock);
516
517	rc = idr_alloc(&memstick_host_idr, host, 0, 0, GFP_NOWAIT);
518	if (rc >= 0)
519		host->id = rc;
520
521	spin_unlock(&memstick_host_lock);
522	idr_preload_end();
523	if (rc < 0)
524		return rc;
525
526	dev_set_name(&host->dev, "memstick%u", host->id);
527
528	rc = device_add(&host->dev);
529	if (rc) {
530		spin_lock(&memstick_host_lock);
531		idr_remove(&memstick_host_idr, host->id);
532		spin_unlock(&memstick_host_lock);
533		return rc;
534	}
535
536	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
537	memstick_detect_change(host);
538	return 0;
539}
540EXPORT_SYMBOL(memstick_add_host);
541
542/**
543 * memstick_remove_host - stop request processing on memstick host
544 * @host - host to use
545 */
546void memstick_remove_host(struct memstick_host *host)
547{
 
548	flush_workqueue(workqueue);
549	mutex_lock(&host->lock);
550	if (host->card)
551		device_unregister(&host->card->dev);
552	host->card = NULL;
553	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
554	mutex_unlock(&host->lock);
555
556	spin_lock(&memstick_host_lock);
557	idr_remove(&memstick_host_idr, host->id);
558	spin_unlock(&memstick_host_lock);
559	device_del(&host->dev);
560}
561EXPORT_SYMBOL(memstick_remove_host);
562
563/**
564 * memstick_free_host - free memstick host
565 * @host - host to use
566 */
567void memstick_free_host(struct memstick_host *host)
568{
569	mutex_destroy(&host->lock);
570	put_device(&host->dev);
571}
572EXPORT_SYMBOL(memstick_free_host);
573
574/**
575 * memstick_suspend_host - notify bus driver of host suspension
576 * @host - host to use
577 */
578void memstick_suspend_host(struct memstick_host *host)
579{
580	mutex_lock(&host->lock);
581	host->set_param(host, MEMSTICK_POWER, MEMSTICK_POWER_OFF);
582	mutex_unlock(&host->lock);
583}
584EXPORT_SYMBOL(memstick_suspend_host);
585
586/**
587 * memstick_resume_host - notify bus driver of host resumption
588 * @host - host to use
589 */
590void memstick_resume_host(struct memstick_host *host)
591{
592	int rc = 0;
593
594	mutex_lock(&host->lock);
595	if (host->card)
596		rc = memstick_power_on(host);
597	mutex_unlock(&host->lock);
598
599	if (!rc)
600		memstick_detect_change(host);
601}
602EXPORT_SYMBOL(memstick_resume_host);
603
604int memstick_register_driver(struct memstick_driver *drv)
605{
606	drv->driver.bus = &memstick_bus_type;
607
608	return driver_register(&drv->driver);
609}
610EXPORT_SYMBOL(memstick_register_driver);
611
612void memstick_unregister_driver(struct memstick_driver *drv)
613{
614	driver_unregister(&drv->driver);
615}
616EXPORT_SYMBOL(memstick_unregister_driver);
617
618
619static int __init memstick_init(void)
620{
621	int rc;
622
623	workqueue = create_freezable_workqueue("kmemstick");
624	if (!workqueue)
625		return -ENOMEM;
626
627	rc = bus_register(&memstick_bus_type);
628	if (rc)
629		goto error_destroy_workqueue;
630
631	rc = class_register(&memstick_host_class);
632	if (rc)
633		goto error_bus_unregister;
634
635	return 0;
636
637error_bus_unregister:
638	bus_unregister(&memstick_bus_type);
639error_destroy_workqueue:
640	destroy_workqueue(workqueue);
641
642	return rc;
643}
644
645static void __exit memstick_exit(void)
646{
647	class_unregister(&memstick_host_class);
648	bus_unregister(&memstick_bus_type);
649	destroy_workqueue(workqueue);
650	idr_destroy(&memstick_host_idr);
651}
652
653module_init(memstick_init);
654module_exit(memstick_exit);
655
656MODULE_AUTHOR("Alex Dubov");
657MODULE_LICENSE("GPL");
658MODULE_DESCRIPTION("Sony MemoryStick core driver");