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